package com.xiangban.service.impl;

import com.xiangban.dao.FrontPageMapper;
import com.xiangban.model.Relation;
import com.xiangban.model.dto.RelationDto;
import com.xiangban.service.FrontPageService;
import com.xiangban.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Author Mr. MaYaYun
 * @Data 2021/12/28 9:01
 */
@Service
public class FrontPageServiceImpl implements FrontPageService {

    @Autowired
    private FrontPageMapper frontPageMapper;
    @Autowired
    private CommonUtils commonUtils;

    /**
     * @return 顶部数据栏
     */
    @Override
    public Object variousData() {
        HashMap<String, Integer> map = new HashMap<>();

        //今日订单数量
        Integer todayOrdersNumber = frontPageMapper.todayOrdersNumber();
        map.put("todayOrdersNumber",todayOrdersNumber);

        //今日新增用户数量
        int todayRelationNumber = frontPageMapper.todayRelationNumber();
        map.put("todayRelationNumber",todayRelationNumber);

        //陪诊总数
        int accompanyNumber = frontPageMapper.accompanyNumber();
        map.put("accompanyNumber",accompanyNumber);

        //用户总数
        List<Relation> relationNumber = frontPageMapper.relationNumber();
        map.put("relationNumber",relationNumber.size());

        //订单总数
        int orderNumber = frontPageMapper.orderNumber();
        map.put("orderNumber",orderNumber);

        return map;
    }

    /**
     * @return 年龄分布统计
     */
    @Override
    public Object ageStatistics() {
        ArrayList<Object> list = new ArrayList<>();
        HashMap<String, Object> map1 = new HashMap<>();
        HashMap<String, Object> map2 = new HashMap<>();
        HashMap<String, Object> map3 = new HashMap<>();
        HashMap<String, Object> map4 = new HashMap<>();
        HashMap<String, Object> map5 = new HashMap<>();
        int underTwenty = 0;
        int twentyToForty = 0;
        int fortyToSixty = 0;
        int sixtyToEighty = 0;
        int overEighty = 0;
        List<Relation> relationNumber = frontPageMapper.relationNumber();
        for (Relation relation : relationNumber) {
            int age = commonUtils.getAgeByIdCard(relation.getIdCard());
            if (age <20){
                underTwenty++;
            }else if (age >=20 && age <40){
                twentyToForty++;
            }else if (age >=40 && age <60){
                fortyToSixty++;
            }else if (age >=60 && age <80){
                sixtyToEighty++;
            }else if (age >=80){
                overEighty++;
            }
        }
        map1.put("name","20以下");
        map1.put("value",underTwenty);

        map2.put("name","20~40");
        map2.put("value",twentyToForty);

        map3.put("name","40~60");
        map3.put("value",fortyToSixty);

        map4.put("name","60~80");
        map4.put("value",sixtyToEighty);

        map5.put("name","80以上");
        map5.put("value",overEighty);
        list.add(map1);
        list.add(map2);
        list.add(map3);
        list.add(map4);
        list.add(map5);
        return list;
    }

    /**
     * @return 用户来源分布
     */
    @Override
    public Object relationSource() {
        ArrayList<Object> list = new ArrayList<>();
        HashMap<String, Object> map1 = new HashMap<>();
        HashMap<String, Object> map2 = new HashMap<>();
        HashMap<String, Object> map3 = new HashMap<>();
        HashMap<String, Object> map4 = new HashMap<>();
        int mobile = 0;
        int disabledPersonsFederation = 0;
        int healthCommission = 0;
        int ownExpense = 0;
        List<Relation> relationNumber = frontPageMapper.relationNumber();
        for (Relation relation : relationNumber) {
            if (relation.getSource() == 1){
                mobile++;
            }else if (relation.getSource() == 2){
                disabledPersonsFederation++;
            }else if (relation.getSource() == 3){
                healthCommission++;
            }else if (relation.getSource() == 5){
                ownExpense++;
            }
        }

        map1.put("name","移动端");
        map1.put("value",mobile);

        map2.put("name","残联");
        map2.put("value",disabledPersonsFederation);

        map3.put("name","卫健委");
        map3.put("value",healthCommission);

        map4.put("name","自费");
        map4.put("value",ownExpense);

        list.add(map1);
        list.add(map2);
        list.add(map3);
        list.add(map4);
        return list;
    }

    /**
     * @return 用户订单排行榜
     */
    @Override
    public Object relationOrderRanking() {
        HashMap<String, Object> map = new HashMap<>();
        ArrayList<String> listName = new ArrayList<>();
        ArrayList<Integer> listOrderTotal = new ArrayList<>();
        List<HashMap<String, Object>> hashMaps = frontPageMapper.relationOrderRanking();
        for (HashMap<String, Object> hashMap : hashMaps) {
            listName.add(hashMap.get("name").toString());
            listOrderTotal.add(Integer.parseInt(hashMap.get("orderTotal").toString()));
        }
        map.put("name",listName);
        map.put("orderTotal",listOrderTotal);
        return map;
    }

    /**
     * @return 医院订单排行
     */
    @Override
    public Object hospitalOrderRanking() {
        HashMap<String, Object> map = new HashMap<>();
        ArrayList<String> listName = new ArrayList<>();
        ArrayList<Integer> listOrderTotal = new ArrayList<>();
        List<HashMap<String, Object>> hashMaps = frontPageMapper.hospitalOrderRanking();
        for (HashMap<String, Object> hashMap : hashMaps) {
            listName.add(hashMap.get("name").toString());
            listOrderTotal.add(Integer.parseInt(hashMap.get("orderTotal").toString()));
        }
        map.put("name",listName);
        map.put("orderTotal",listOrderTotal);
        return map;
    }

    /**
     * @return 今日生日用户
     */
    @Override
    public Object birthdayToday() {
        List<RelationDto> relations = frontPageMapper.birthdayToday();
        for (RelationDto relation : relations) {
            int age = commonUtils.getAgeByIdCard(relation.getIdCard());
            relation.setAge(age);
        }
        return relations;
    }
}

